<template>
  <el-dialog title="安全认证" :visible.sync="visible" :append-to-body="true" :show-close="true" :close-on-click-modal="true" :close-on-press-escape="true">
    <el-form ref="inputForm" :model="inputForm"
      label-width="80px" @submit.native.prevent>
      <div style="width: 100%; text-align: center; font-size: 16px; color: red; line-height: 70px;">{{notice}}</div>
      <el-form-item label="姓名">
        <span>{{ userName }}</span>
      </el-form-item>
      <el-form-item label="登录名">
        <span>{{ loginName }}</span>
      </el-form-item>
      <el-form-item label="登录密码" prop="password" :rules="[
          {required: true, message:'密码不能为空', trigger:'blur'}
         ]">
        <el-input type="password" v-model="inputForm.password" show-password></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button  @click="visible = false">关闭</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {clearLoginInfo} from '@/utils'
  export default {
    data() {
      return {
        visible: false,
        loading: false,
        resolve: null,
        reject: null,
        notice: '当前操作可能会影响系统的正常运行，确认操作，请输入登录密码',
        inputForm: {
          password: ''
        }
      }
    },
    computed: {
      userName: {
        get() {
          return this.$store.state.user.name
        }
      },
      loginName: {
        get() {
          return this.$store.state.user.loginName
        }
      }
    },
    methods: {
      // 初始化
      async open(notice) {
        this.visible = true
        this.loading = false;
        this.inputForm.password = '';
        if(notice && notice != '') {
          this.notice = notice;
        }
        return await this.waitForResult()
      },
      waitForResult() {
        var self = this;
        return new Promise((resolve, reject) => {
          self.resolve = resolve;
          self.reject =  reject;
        });
      },
      dataFormSubmit() {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: '/sys/validatePassword',
              method: 'post',
              useAes: true,
              data: {
                'userName': this.loginName,
                'password': this.inputForm.password
              }
            }).then(({data}) => {
              this.loading = false
              // console.log(data)
              if (data && data.success) {
                this.visible = false
                this.resolve(true);
              } else if (data && data.code == -2) {
                clearLoginInfo()
                this.$router.replace({name: 'login'})
              }
            })
          }
        })
      }
    }
  }
</script>
